package com.wushi.lenovo.asia5b.app_ui.other.dialog;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;

import com.mvp.base.BaseDialog;
import com.mvp.utils.AppUtil;
import com.wushi.lenovo.asia5b.BuildConfig;
import com.wushi.lenovo.asia5b.R;
import com.wushi.lenovo.asia5b.app_model.other.UpdateModel;

import butterknife.BindView;

/**
 * Created by zhangyuncai on 2020/5/9.
 */
public class UpdateDialog extends BaseDialog {

    @BindView(R.id.tv_update_content)
    TextView tv_update_content;
    @BindView(R.id.tv_update_cancel)
    TextView tv_update_cancel;
    @BindView(R.id.view_update_middleline)
    View view_update_middleline;
    @BindView(R.id.tv_update_sure)
    TextView tv_update_sure;
    private final UpdateModel updateModel;

    public UpdateDialog(UpdateModel updateModel, Context context) {
        super(context);
        this.updateModel = updateModel;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_update;
    }

    @Override
    protected void initView() {
        tv_update_content.setText(updateModel.getChangelog());
        setCanceledOnTouchOutside(false);
        if (TextUtils.equals(updateModel.getForce_update(), "1")) {//强制升级
            setCancelable(false);
            view_update_middleline.setVisibility(View.GONE);
            tv_update_cancel.setVisibility(View.GONE);
        } else {
            tv_update_cancel.setOnClickListener(v -> {
                dismiss();
            });
        }
        tv_update_sure.setOnClickListener(v -> {
            if (BuildConfig.appType == 0) {//有跳转地址就跳跳转地址
                String downloadUrl = "http://120.78.208.222/download.html?type=newasia5b_cn";
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(downloadUrl));
                getContext().startActivity(intent);
            } else {//否则直接跳谷歌商城
                AppUtil.INSTANCE.downloadApp(getContext(), AppUtil.INSTANCE.getPackageName());
            }
        });
    }

    @Override
    protected void initListener() {

    }

    @Override
    protected int getWidthStyle() {
        return getScreenWidth(0.8f);
    }

    @Override
    protected int getGravity() {
        return Gravity.CENTER;
    }
}
